package dfbz.com.controller.view;

import dfbz.com.constant.Const;
import dfbz.com.controller.BaseServlet;
import dfbz.com.service.UserService;
import dfbz.com.service.impl.UserServiceImpl;
import dfbz.com.utils.PageUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    public void index(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void forget(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/forget.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void home(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //跳主页jsp，顺便去后台查询用户的基本信息
            Object obj = req.getSession().getAttribute("userId");
            if (null != obj) {
                Integer userId = Integer.parseInt(obj.toString());
                Map<String, Object> map = userService.findUserBaseById(userId);
                req.setAttribute("userBaseInfo", map);
            }
            req.getRequestDispatcher("/html/home.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void showRegister(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void userList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("page");
            String search = request.getParameter("search");
            if(null != search){
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = request.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = userService.findList(Integer.parseInt(userId), page, search);
            request.setAttribute("userList", list);
            long totalPage = PageUtil.getTotalPage(userService.findTotalUserCount());
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("currentPage", page);
            request.setAttribute("startPage", startPage);
            request.setAttribute("endPage", endPage);
            request.setAttribute("search", search);
            //再去后台查询用户的基本信息
            Map<String, Object> map = userService.findUserBaseById(Integer.parseInt(userId));
            request.setAttribute("userBaseInfo", map);
            request.getRequestDispatcher("/html/user.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
